package com.demo.apparel.service.web.Impl;

import com.demo.apparel.dto.web.AdminInfoUpdateDTO;
import com.demo.apparel.dto.web.AdminQueryDTO;
import com.demo.apparel.entity.AdminInfo;
import com.demo.apparel.mapper.web.AdminInfoMapper;
import com.demo.apparel.service.web.AdminInfoService;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.web.AdminVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class AdminInfoServiceImpl implements AdminInfoService {

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Override
    public AdminInfo getInfo(String adminId) {
        AdminInfo info = adminInfoMapper.selectByAdminId(adminId);
        if (info == null) {
            // 若没有信息则初始化一条空记录
            info = new AdminInfo();
            info.setId(UUID.randomUUID().toString().replace("-", ""));
            info.setAdminId(adminId);
            adminInfoMapper.insert(info);
        }
        return info;
    }

    @Override
    public void updateInfo(String adminId, AdminInfoUpdateDTO dto) {
        AdminInfo update = new AdminInfo();
        update.setAdminId(adminId);
        update.setNickname(dto.getNickname());
        update.setAvatar(dto.getAvatar());
        update.setEmail(dto.getEmail());
        update.setGender(dto.getGender());
        adminInfoMapper.updateByAdminId(update);
    }

    /**
     *post管路员用户列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<AdminVO> getAdminList(AdminQueryDTO dto) {
        int offset = (dto.getPageNum() - 1) * dto.getPageSize();
        List<AdminVO> records = adminInfoMapper.selectAdmins(dto.getPhone(), dto.getNickname(), offset, dto.getPageSize());
        long total = adminInfoMapper.countAdmins(dto.getPhone(), dto.getNickname());
        return new PageVO<>(records, total, dto.getPageNum(), dto.getPageSize());
    }


}
